package com.fangs.lunbotu.banner;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;

import com.fangs.lunbotu.utils.DisplayUtils;


public class MyVideoLayout extends RelativeLayout implements LifecycleObserver {

    public MyBannerVideoView videoView;
    public ImageView imageView;
    private ProgressBar progressBar;
    private boolean loop;//是否循环播放
    private int videoCurrentPosition;
    public int videoLimitTime;
    private boolean playing;//标记是否是页面失去焦点时正在播放的视频 那么这个视频在获得焦点时需要恢复播放
    private boolean buffering; //正在缓冲中

    public MyVideoLayout(Context context, boolean loop) {
        this(context);
        this.loop = loop;
        init();
    }

    private MyVideoLayout(Context context) {
        super(context);
    }

    public MyVideoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.loop = true;
        init();
    }

    public MyVideoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.loop = true;
        init();
    }

    private void init() {
        if (getContext() instanceof AppCompatActivity) {
            ((AppCompatActivity) getContext()).getLifecycle().addObserver(this);
        }

        videoView = new MyBannerVideoView(getContext(), loop);
        videoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        videoView.setShowLoadingListener(new MyBannerVideoView.ShowLoadingListener() {
            @Override
            public void showLoading(boolean show) {
                imageView.setVisibility(show ? VISIBLE : GONE);
                progressBar.setVisibility(show ? VISIBLE : GONE);
            }

            @Override
            public void showBuffering(boolean show) {
                progressBar.setVisibility(show ? VISIBLE : GONE);
                buffering = show;
            }
        });
        addView(videoView);

        imageView = new ImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(imageView);

        progressBar = new ProgressBar(getContext());
        int w = DisplayUtils.dip2px(getContext(), 45);
        LayoutParams layoutParams = new LayoutParams(w, w);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        progressBar.setIndeterminateDrawable(getContext().getResources().getDrawable(cn.jzvd.R.drawable.jz_loading)); //此处借用饺子视频中的进度样式
        progressBar.setLayoutParams(layoutParams);
        addView(progressBar);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume(LifecycleOwner lifecycleOwner) {
        if (playing) {
            // 笨办法 解决进入下一页后通过侧滑返回首页时 videoview异常问题(黑屏，显示不全)
            imageView.setVisibility(VISIBLE);
            progressBar.setVisibility(VISIBLE);
            videoView.setScaleError();
            videoView.seekTo(videoCurrentPosition);
            videoView.start();
            playing = false;
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onPause(LifecycleOwner lifecycleOwner) {
        if (videoView.isPlaying() && !buffering) {
            playing = true;
            videoCurrentPosition = videoView.getCurrentPosition();
            videoLimitTime = videoView.getDuration() - videoCurrentPosition;
            videoView.pause();
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void onDestroy(LifecycleOwner lifecycleOwner) {
        videoView.stopPlayback();
    }

    @Override
    protected void onDetachedFromWindow() { //避免viewpager item view复用情况 视频黑屏
        super.onDetachedFromWindow();
        imageView.setVisibility(VISIBLE);
        progressBar.setVisibility(VISIBLE);
    }
}
